<?php
require_once ROOT_LIBRARY_UTILITY.DS. 'BaseController.php';
require_once ROOT_LIBRARY_UTILITY.DS. 'Globals.php';

require_once ROOT_APPLICATION_MODULE_BACKEND_MODELS.DS. 'Model_Article_Type.php';
require_once ROOT_APPLICATION_MODULE_BACKEND_FORMS.DS. 'Form_Article_Type.php';
require_once ROOT_APPLICATION_OBJECT.DS. 'Article_Type.php';

class Backend_ArticletypeController extends BaseController
{

    public function init(){
        parent::init();
        $this->_helper->layout->setLayout('backend');
    }

    public function  preDispatch() {
        parent::preDispatch();
    }

    public function indexAction(){
        try
    	{
            $this->view->action = "index";
            //for pagination
            $mdArticleType = new Model_Article_Type();
            $select = $mdArticleType->getDataForPagination();

            $paginator = Zend_Paginator::factory($select);
            $page = $this->_request->getParam('page') ? $this->_request->getParam('page') : 1;
            $paginator->setCurrentPageNumber($page);
            $paginator->setItemCountPerPage(DEFAULE_ITEMS_IN_LIST);

            $this->view->paginator = $paginator;

            $langSession    = new Zend_Session_Namespace('language');

            if ($this->_request->getMethod () == "POST")
            {

            }
            elseif($this->_request->getMethod() == "GET")
            {
                $paramLang = $this->_request->getParam('lang');
                Globals::setLanguage($paramLang);
                Globals::getLanguage($this);
            }
    	}
    	catch (Exception  $ex)
    	{
    	}
    }



    public function addAction()
    {
    	try
    	{
            $mdArticleType  = new Model_Article_Type();
            $form       = new Form_Article_Type();

            $langSession    = new Zend_Session_Namespace('language');

            if ($this->_request->getMethod() == "POST")
            {
                $form->_requestToForm($this);
                $form->validate(INSERT);

                //insert to database
                $mdArticleType->_insert($form->obj);
                $this->_redirect('/backend/articletype');
            }
            elseif($this->_request->getMethod() == "GET")
            {
                $paramLang = $this->_request->getParam('lang');
                Globals::setLanguage($paramLang);
                Globals::getLanguage($this);

                //get all service types
                $this->view->articleTypes = $mdArticleType->_getAll();
            }
    	}
        catch(Zend_File_Transfer_Exception $ex)
        {
            $this->view->errMsg = $ex->getMessage();
        }
    	catch (Exception  $ex)
    	{
            $this->view->errMsg = $ex->getMessage();
    	}
    }


    public function editAction()
    {
        try
    	{
            $mdArticleType  = new Model_Article_Type();
            $form       = new Form_Article_Type();


            $hidId = $this->_request->getParam('id');
            $obj = $mdArticleType->_getByKey($hidId);

            $langSession    = new Zend_Session_Namespace('language');

            if ($this->_request->getMethod () == "POST")
            {
                $form->_requestToForm($this);
                $form->validate(UPDATE);

                $mdArticleType->_update($form->obj);
                $this->_redirect('/backend/articletype');
            }
            elseif($this->_request->getMethod() == "GET")
            {
                $this->view->obj = $obj;

                $paramLang = $this->_request->getParam('lang');
                Globals::setLanguage($paramLang);
                Globals::getLanguage($this);

                //get all service types
                $this->view->articleTypes = $mdArticleType->_getAll();
            }
    	}
        catch(Zend_File_Transfer_Exception $ex)
        {
            $this->view->errMsg = $ex->getMessage();
        }
    	catch (Exception  $ex)
    	{

    	}
    }


    public function deleteAction()
    {
    	$this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $articletypeId	= $this->_request->getParam('id');

        if ($this->_request->getMethod() == "POST")
        {
            $mdArticleType  = new Model_Article_Type();
            $arrId	= explode(',',$articletypeId);


            if (count($arrId) >= 1)
                foreach ($arrId as $id)
                {
                    if((int)$id != 0)
                    {
                        $result = $mdArticleType->_delete($id);
                        var_dump($result);
                    }
                }
        }
    }
}
?>